package com.chinasofti.project.web;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chinasofti.project.common.pageQuery.PageResult;
import com.chinasofti.project.common.result.ExecuteResult;
import com.chinasofti.project.service.product.ProductService;
import com.chinasofti.project.web.common.ControllerBase;

@Controller
public class ProductController extends ControllerBase {

	@Autowired
	private ProductService productService;

	/**
	 * 添加到收藏夹
	 * 
	 * @param session
	 * @param productId
	 * @return
	 */
	@RequestMapping("addFavorite")
	@ResponseBody
	private ExecuteResult addFavorite(HttpSession session, int productId) {
		int userId = (int) session.getAttribute("userId");
		productService.addFavorite(userId, productId);
		return ExecuteResult.successResult();
	}

	/**
	 * 进入收藏夹以及翻页
	 * 
	 * @param session
	 * @param mm
	 * @param pageIdx
	 * @return
	 */
	@RequestMapping("toFavorite")
	private String toFavorite(HttpSession session, ModelMap mm, int pageIdx) {
		int userId = (int) session.getAttribute("userId");
		PageResult<Map> favoriteResultes = productService.selecteFavoriteByUserId(userId, pageIdx);
		mm.put("favoriteResultes", favoriteResultes);
		mm.put("pageIdx", pageIdx);
		return "breeze_favorite";
	}

	/**
	 * 删除收藏夹
	 * 
	 * @param favoriteId
	 * @param session
	 * @param mm
	 * @return
	 */
	@RequestMapping("deleteFavorite")
	private String deleteFavorite(int favoriteId, HttpSession session, ModelMap mm) {
		int i = productService.deleteFavorite(favoriteId);
		return toFavorite(session, mm, 1);
	}

}
